//
//  ColorfulEggViewController.m
//  jingjing2.0
//
//  Created by apple on 16/5/24.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "ColorfulEggViewController.h"
#import "EggAnimationView.h"
#import "GetEggRewardViewController.h"
@interface ColorfulEggViewController ()
{
    EggAnimationView *_eggView;
    NSTimer *_timer;
    NSString *_eggID;
}

@end

@implementation ColorfulEggViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor=[UIColor whiteColor];
    self.bgView.hidden=YES;
    //圆角半径
    self.clickEggBtn.layer.cornerRadius=4;
    self.clickEggBtn.clipsToBounds=YES;
    //添加下划线
    NSMutableAttributedString *content = [[NSMutableAttributedString alloc]initWithString:@"什么是净净彩蛋？"];
    NSRange contentRange = {0,[content length]};
    [content addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:contentRange];
     [content addAttribute:NSForegroundColorAttributeName value:BLUECOLOR range:contentRange];
    [_aboutCaiDan setAttributedTitle:content forState:UIControlStateNormal];
    //请求数据
   
     _timer=[NSTimer scheduledTimerWithTimeInterval:0.5 target:self selector:@selector(progressViewAddValue) userInfo:nil repeats:YES];
    [_timer setFireDate:[NSDate distantFuture]];
   }
#pragma mark--获取数据
-(void)getData
{
    //    http://192.168.1.107:3722/Activity/ColorEggHome/{UserCode}/{Timestamp}/{Sign}
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSMutableAttributedString *content = [[NSMutableAttributedString alloc]initWithString:[NSString stringWithFormat:@"您的待办业务条fasdfasdfosadfjasdkljfklasjklfjasdlkjfklasdjklfjalskdjflkadsj"]];
    NSRange contentRange = {0,[content length]};
    [content addAttribute:NSUnderlineStyleAttributeName value:[NSNumber numberWithInteger:NSUnderlineStyleSingle] range:contentRange];
    NSString *timeStamp=[GeneralTool gettimestamp];
    NSDictionary *dic=@{@"UserCode":GETUSERCODE,
                        @"Timestamp":timeStamp,
                        };
    NSString *sign=[GeneralTool GetSin:dic];
    NSString *urlString=[NSString stringWithFormat:@"Activity/ColorEggHome/%@/%@/%@",GETUSERCODE,timeStamp,sign];
    [RequestTool requestDataWithType:@"get" andUrlString:urlString andParameters:nil andRequestFinished:^(id responses, NSError *error) {
        [MBProgressHUD hideHUDForView:self.view];
        if ([responses[@"IsSuccess"]integerValue]==1) {
             NSDictionary *dic=responses[@"ReturnObject"];
            if ([dic[@"EggNum"] isEqualToString:@"0"]) {
                _clickEggBtn.backgroundColor=GRAYCOLOR;
            }else
            {
                 _eggNumLbl.text=dic[@"EggNum"];
            }
           
            [_image sd_setImageWithURL:[NSURL URLWithString:dic[@"HomeImgUrl"]] placeholderImage:nil options:SDWebImageRetryFailed];
            _titleLabel.text=dic[@"Title"];
        }
    }];
}
-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    [GeneralTool hideTabBar];
     [self getData];
}
-(void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [GeneralTool showTabBar];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


//关于净净彩蛋
- (IBAction)clickOnAboutJingJingEgg:(id)sender {
    WebViewController *web=[[WebViewController alloc]init];
    web.title=@"净净彩蛋";
    web.hidesBottomBarWhenPushed=YES;
    web.pageUrl=ABOUT_ColorEgg;
    [self.navigationController pushViewController:web animated:YES];
}
//砸蛋
- (IBAction)clickOnEggBtn:(id)sender {
    UIButton *btn=sender;
    _bgView.hidden=NO;
    if (btn.backgroundColor==GRAYCOLOR) {
        [MBProgressHUD showError:@"您还没有彩蛋，快去下单赢取彩蛋吧～"];
    }
     CGFloat width=kHeight/2.0-20;
    CGFloat internal=(kWidth-width)/2.0;
   _eggView=[[EggAnimationView alloc]initWithFrame:CGRectMake(internal+30, self.view.center.y-(width/2.0), width-60, width)];
    [self.view addSubview:_eggView];
    
    [_eggView.animationEgg startAnimating];
    
    //砸个蛋获取一个蛋
    [self getEggID];
    
   [_timer setFireDate:[NSDate distantPast]];
    
    
    
    
}
-(void)getEggID
{
    //http://192.168.1.107:3722/Activity/ColorEggsSubmitV2
    //m_ColorEggsSubmitPara(UserCode,OrderID,Timestamp,Sign)
    //orderID：从订单详情点击砸蛋传订单编号；从其他地方砸蛋传空，传空值时不参与签名。
    NSString *timeStamp=[GeneralTool gettimestamp];
    NSDictionary *dic=@{@"UserCode":GETUSERCODE,
                        @"Timestamp":timeStamp};
    NSString *sign=[GeneralTool GetSin:dic];
    NSDictionary *dataDic=@{@"UserCode":GETUSERCODE,
                            @"OrderID":@"",
                            @"Timestamp":timeStamp,
                            @"Sign":sign};
    NSDictionary *requestData=@{@"m_ColorEggsSubmitPara":dataDic};
    [RequestTool requestDataWithType:@"post" andUrlString:@"Activity/ColorEggsSubmitV2" andParameters:requestData andRequestFinished:^(id responses, NSError *error) {
        if (error) {
            [MBProgressHUD showError:@"无法获取奖励详情，请稍后再次尝试"];
        }
        if ([responses[@"IsSuccess"]integerValue]==1) {
            NSDictionary *dic=responses[@"ReturnObject"];
            _eggID=dic[@"EggID"];
        }else
        {
            [MBProgressHUD showError:responses[@"ReturnString"]];
        }
    }];

}
-(void)progressViewAddValue
{
    _eggView.progressBarView.progress+=0.1;
    if (_eggView.progressBarView.progress>=1 && _eggID) {
        [_timer setFireDate:[NSDate distantFuture]];
        [_eggView.animationEgg stopAnimating];
        _eggView.progressBarView.progress=1;
        [_eggView removeFromSuperview];
        _bgView.hidden=YES;
        GetEggRewardViewController *reward=[[GetEggRewardViewController alloc]init];
        reward.hidesBottomBarWhenPushed=YES;
        reward.eggID=_eggID;
        [self.navigationController pushViewController:reward animated:YES];
    }
}
@end
